package com.eco.fanliapp.admin.controller.web;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eco.fanliapp.admin.common.BaseController;
import com.eco.fanliapp.common.response.ServerResponse;
import com.eco.fanliapp.entity.AppUser;
import com.eco.fanliapp.entity.SyTimePeriodGoods;
import com.eco.fanliapp.service.ISyTimePeriodGoodsService;
import com.eco.fanliapp.utils.DateFormatUtils;
import com.eco.fanliapp.vo.SyTimePeriodGoodsVo;
import com.eco.fanliapp.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2019-09-09
 */
@RestController
@RequestMapping("/web/syTimePeriodGoods")
public class SyTimePeriodGoodsWebController extends BaseController {

    @Autowired
    private ISyTimePeriodGoodsService iSyTimePeriodGoodsService;

    @GetMapping(value = "list")
    public ServerResponse list(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize,Integer tableId,String periodGoodsTime) {
        if(StringUtils.isEmpty(periodGoodsTime)){
            periodGoodsTime = DateFormatUtils.getNowDateDay(new Date());
        }
        pageNum = (pageNum -1)*pageSize;
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("enable", 1);
        queryWrapper.eq("deleted", 0);
        queryWrapper.eq("period_id", tableId);
        queryWrapper.like("period_goods_time",periodGoodsTime);
       /* Page<SyTimePeriodGoods> page = new Page<>(pageNum, pageSize);
        IPage iPage = iSyTimePeriodGoodsService.page(page, queryWrapper);*/
        List<SyTimePeriodGoodsVo>  syTimePeriodGoodsList = iSyTimePeriodGoodsService.selectLimitList(pageNum,pageSize,tableId,periodGoodsTime);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("total",iSyTimePeriodGoodsService.count(queryWrapper));
        jsonObject.put("syTimePeriodGoodsList",syTimePeriodGoodsList);
        jsonObject.put("size",syTimePeriodGoodsList.size());
        jsonObject.put("pageNum",pageNum);
        return ServerResponse.createBySuccess("查询成功", jsonObject);
    }
    @PostMapping(value = "save")
    public ServerResponse save(@RequestBody SyTimePeriodGoods syTimePeriodGoods) {
        UserVO userVO = getAuthentication();
        return iSyTimePeriodGoodsService.saveSyTimePeriodGoods(syTimePeriodGoods,userVO.getUserId());
    }

    /**
     * 第三方添加秒杀商品
     * @param date
     * @param hour
     * @param itemId
     * @return
     */
    @PostMapping(value = "/third/save")
    public ServerResponse thirdSave(String date,String hour,String itemId) {
        return iSyTimePeriodGoodsService.thirdSave(date,hour,itemId);

    }
}
